package com.yumeko.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: First_Generation_Cloud
 * @description:
 * @author: Zheng Hao
 * @create: 2020-04-20 16:47
 */
@RestController
@RequestMapping("/autodeliver")
public class AutodeliverController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    // /autodeliver/checkState/1
    //没有euraka的时候 普通resttemplate调用
    @GetMapping("/checkState0/{userId}")
    public Integer findResumeOpenState0(@PathVariable("userId") Long userId) {
        return restTemplate.getForObject("http://192.168.1.101:1111/resume/openstate/" + userId, Integer.class);
    }

    // /autodeliver/checkState1/1
    //eureka的 discoveryClient调用
    @GetMapping("/checkState1/{userId}")
    public Integer findResumeOpenState1(@PathVariable("userId") Long userId){
        //1获取Eureka的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("SERVICE-RESUME");
        //2获取实例(不考虑负载)
        ServiceInstance serviceInstance = instances.get(0);
        //3根据实例信息拼接请求地址
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();
        serviceInstance.getUri();
        String url = "http://"+host+":"+ port+"/resume/openstate/"+userId;
        //4消费者直接调用服务者
        ResponseEntity<Integer> forEntity = restTemplate.getForEntity(url, Integer.class);
        return forEntity.getBody();
    }

    // 使用ribbon负载调用
    @GetMapping("/checkState2/{userId}")
    public Integer findResumeOpenState(@PathVariable("userId") Long userId) {
        return restTemplate.getForObject("http://service-resume/resume/openstate/" + userId, Integer.class);
    }

/*    //使⽤@HystrixCommand注解进⾏熔断控制
    @HystrixCommand(
            // commandProperties熔断的⼀些细节属性配置
            commandProperties = {
                    // 每⼀个属性都是⼀个HystrixProperty
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value="2000"),
                    //根据以下自定义配置设置跳闸 自我修复
                    // 统计时间窗口定义 默认5秒
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    // 统计时间窗口内的最⼩请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),
                    // 统计时间窗口内的错误数量百分⽐阈值
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    // ⾃我修复时的活动窗口调度
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            },
            //资源隔离 线程占用满  仓壁模式
            // 线程池标识，要保持唯⼀，不唯⼀的话就共⽤了
            threadPoolKey = "findResumeOpenState2",
            // 线程池细节属性配置 默认10个线程池
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize" , value="2"), // 线程数
                    @HystrixProperty(name = "maxQueueSize",value = "20"), // 等待队列
            },
            // 回退⽅法
            fallbackMethod = "myFallBack"
    )
    @GetMapping("/checkState3/{userId}")
    public Integer findResumeOpenState2(@PathVariable("userId") Long userId) {
        return restTemplate.getForObject("http://service-resume/resume/openstate1/" + userId, Integer.class);
    }


    //定义回退⽅法，返回预设默认值
    //注意：该⽅法形参和返回值与原始⽅法保持⼀致
    public Integer myFallBack(Long userId) {
        return -123333; // 兜底数据
    }*/
}
